private void ManageNightLights() { Entity(HallPir !) .StateChanges .Where(e => e.New?.State == "on" && e.Old?.State == "off" && (IsNight || IsMorning || IsEvening)) .Subscribe( s => { Light.HallTak1.TurnOn(new { transition = 0 }); Light.HallTak2.TurnOn(new { transition = 0 }); } ); Entity(HallPir !) .StateChanges .Where(e => e.New?.State == "off" && e.Old?.State == "on" && (IsNight || IsMorning || IsEvening) && !IsTimeNowBetween(TimeSpan.FromHours(5), TimeSpan.FromHours(10))) .NDSameStateFor(TimeSpan.FromMinutes(1)) .Subscribe( s => { Light.HallTak1.TurnOff(new { transition = 0 }); Light.HallTak2.TurnOff(new { transition = 0 }); } ); }
public override void Initialize() { Entity(AlarmSensor !) .StateChanges.Where(e => e.New.Attribute?.next_alarm_status == "ringing" && e.Old.Attribute?.next_alarm_status != "ringing") .Subscribe(s => { // make a short delay RunIn(TimeSpan.FromSeconds(30), () => Speak("media_player.sovrum", "du har satt larm!")); }); }
public override Task InitializeAsync() { Entity(Calendar !) .StateChanges.Where(e => e.New.State == "on") .Subscribe(s => { Speak("media_player.huset", "Viktigt meddelande"); // Important message Speak("media_player.huset", s.New?.Attribute?.message); Speak("media_player.huset", s.New?.Attribute?.description); }); return(Task.CompletedTask); }
/// <summary> /// Initialize, is automatically run by the daemon /// </summary> public override void Initialize() { // Set up the state management // When chromecast is going off // Entities("media_player.tv_nere") // .StateChanges // .Where(e => e.New?.State == "off" && e.Old?.State is object ) // .Subscribe(s => // { // if (TvIsOn) // { // Log('TV is off, turnin off shield and tv') // RunScript("tv_off_scene"); // } // }); // When state change on my media players, call OnMediaStateChanged Entities(TvMediaPlayers !) .StateChanges .Subscribe(s => { OnMediaStateChanged(s.New, s.Old); }); // When TV on (remote on), call OnTvTurnedOn Entity(RemoteTVRummet !) .StateChanges .Where(e => e.Old?.State == "off" && e.New?.State == "on") .Subscribe(s => { LogDebug("TV remote status change from {from} to {to}", s.Old?.State, s.New?.State); OnTVTurnedOn(); }); // When ever TV remote activity changes, ie TV, Film, Poweroff call OnTvActivityChange Entity(RemoteTVRummet !) .StateAllChanges .Where(e => e.New?.Attribute?.current_activity != e.Old?.Attribute?.current_activity) .Subscribe(s => { LogDebug("TV remote activity change from {from} to {to}", s.Old?.Attribute?.current_activity, s.New?.Attribute?.current_activity); OnTvActivityChange(s.New); }); }
public override Task InitializeAsync() { if (!CheckConfig()) { return(Task.CompletedTask); } Entity(DoorSensor !) .StateChanges .Where(e => e.New?.State == "on") .Subscribe(s => GreetIfJustArrived(s.New.EntityId)); StateChanges .Where( e => e.New.EntityId.EndsWith(PresenceCriteria !) && e.New?.State == "Nyss anlänt") .Subscribe(s => GreetIfJustArrived(s.New.EntityId)); return(Task.CompletedTask); }
/// <summary> /// Initialize, is automatically run by the daemon /// </summary> public override void Initialize() { // Set up the state management // When chromecast is going off // Entities("media_player.tv_nere") // .StateChanges // .Where(e => e.New?.State == "off" && e.Old?.State is object ) // .Subscribe(s => // { // if (TvIsOn) // { // Log('TV is off, turnin off shield and tv') // RunScript("tv_off_scene"); // } // }); // When state change on my media players, call OnMediaStateChanged Entities(TvMediaPlayers !) .StateChanges .Subscribe(s => OnMediaStateChanged(s.New, s.Old)); // When TV on (remote on), call OnTvTurnedOn Entity(RemoteTVRummet !) .StateChanges .Where(e => e.Old?.State == "off" && e.New?.State == "on") .Subscribe(s => OnTVTurnedOn()); // When ever TV remote activity changes, ie TV, Film, Poweroff call OnTvActivityChange Entity(RemoteTVRummet !) .StateAllChanges .Where(e => e.New?.Attribute?.current_activity != e.Old?.Attribute?.current_activity) .Subscribe(s => OnTvActivityChange(s.New)); // This function does not contain any async calls so just return completed task // return Task.CompletedTask; }
/// <summary> /// Initialize the automations /// </summary> /// <remarks> /// - Schedules check every minute if heater should be on or off depending on temperature /// - Set the manually started flag if heater is turn on and not turned on by this script /// </remarks> public override void Initialize() { // Get the state if manually started from statestorage RunEveryMinute(0, () => HandleCarHeater()); Entity(HeaterSwitch !) .StateChanges .Where(e => e.New.State == "on") .Subscribe(s => { if (_appChangedState == false) { // It is manually turned on Storage.IsManualState = true; } else { Storage.IsManualState = false; } _appChangedState = false; }); }
public UptimeReport CalculateUptimePerIntervals() { UptimePerIntervals = RelevantTimespans.Select(relevantTimespan => { var since = CalculatedOn - relevantTimespan; var events = StateChanges.Where(s => s.NobreakState.Timestamp >= since).ToList(); var rightBefore = StateChanges.SkipWhile(s => s.NobreakState.Timestamp >= (events.LastOrDefault()?.NobreakState.Timestamp ?? DateTime.Now)).FirstOrDefault(); if (rightBefore != null) { events.Add(rightBefore); } var sumPerState = PossibleStates.Select(s => new KeyValuePair <PowerStates, TimeSpan>(s, TimeSpan.Zero)).ToDictionary(); for (int i = 0; i < events.Count; i++) { sumPerState[events[i].PowerState] += events[i].NobreakState.Timestamp < since ? events[i].Duration - (CalculatedOn - relevantTimespan - events[i].NobreakState.Timestamp) : events[i].Duration; } var sum = TimeSpan.Zero; foreach (var state in sumPerState) { sum += state.Value; } return(new UptimeInInterval { UptimeStates = sumPerState.Select(stateSum => new UptimeState { ShareTimespan = stateSum.Value, SharePercentage = stateSum.Value / sum * 100, PowerState = stateSum.Key }).ToList(), Since = since, TimeSpan = relevantTimespan }); }).ToList(); return(this); }
public override void Initialize() { _Emoji = Type ! == "Washer" ? "🧼" : "🧺"; Entity(PowerSensor !) .StateChanges .Where( e => e.New?.ToStateBool() == true) .NDSameStateFor(TimeSpan.FromSeconds(90)) .Subscribe(s => Running()); Entity(PowerSensor !) .StateChanges .Where( e => e.New?.ToStateBool() == false) .NDSameStateFor(TimeSpan.FromSeconds(270)) .Subscribe(s => Clean()); Entity(HatchSensor !) .StateChanges .Where( e => e.New?.State == "on") .Subscribe(s => Emptied()); }
/// <summary> /// Initialize, is automatically run by the daemon /// </summary> public override void Initialize() { // Set up the state management // When state change on my media players, call OnMediaStateChanged Entities(TvMediaPlayers !) .StateChanges .Subscribe(s => OnMediaStateChanged(s.New, s.Old)); // When TV on (remote on), call OnTvTurnedOn Entity(RemoteTVRummet !) .StateChanges .Where(e => e.New?.State == "on") .Subscribe(s => OnTVTurnedOn()); // When ever TV remote activity changes, ie TV, Film, Poweroff call OnTvActivityChange Entity(RemoteTVRummet !) .StateAllChanges .Where(e => e.New?.Attribute?.current_activity != e.Old?.Attribute?.current_activity) .Subscribe(s => OnTvActivityChange(s.New)); // This function does not contain any async calls so just return completed task // return Task.CompletedTask; }
/// <summary> /// Initialize the app /// </summary> public void Initialize() { _app.Entity("binary_sensor.kitchen") .StateChanges .Where(e => e.New?.State == "on" && e.Old?.State == "off") .Subscribe(_ => { // Just for the testing of all these _app.Entity("light.kitchen").TurnOn(new { brightness = 100 }); _app.Entity("light.kitchen").TurnOff(new { brightness = 100 }); _app.Entity("light.kitchen").Toggle(new { brightness = 100 }); } ); _app.Entity("binary_sensor.livingroom") .StateChanges .Where(e => e.New?.State == "on" && e.Old?.State == "off") .Subscribe(_ => _app.Entity("sensor.mysensor").SetState(20, new { battery_level = 90 })); _app.Entities(n => n.EntityId !.EndsWith("entities", StringComparison.InvariantCultureIgnoreCase)).StateChanges.Where(s => s.New.State == "on").Subscribe(_ => _app.Entity("light.kitchen").TurnOn()); _app.Entities(n => n.EntityId !.EndsWith("entities", StringComparison.InvariantCultureIgnoreCase)).StateChanges.Where(s => s.New.State == "off").Subscribe(_ => _app.Entity("light.kitchen").TurnOff()); _app.Entity("sensor.temperature") .StateAllChanges .Where(e => e.New?.Attribute?.battery_level < 15) .Subscribe(_ => _app.CallService("notify", "notify", new { title = "Hello from Home Assistant" })); _app.EventChanges .Where(e => e.Event == "hello_event") .Subscribe(_ => _app.Entity("light.livingroom").TurnOn()); _app.EventChanges .Where(e => e.Event == "bye_event") .Subscribe(_ => _app.Entity("light.livingroom").TurnOff()); _app.SetState("sensor.any_sensor", 20, new { battery_level = 70 }); _app.SetState("sensor.any_sensor2", 20, new { battery_level = 70 }); _app.Entity("binary_sensor.test_state_entity") .StateChanges .Subscribe(_ => { if (_app.State("sensor.some_other_entity")?.State == "on") { _app.Entity("light.state_light").TurnOn(); } }); _app.RunIn(TimeSpan.FromMilliseconds(100), () => _app.Entity("binary_sensor.fake_run_in_happened").TurnOn()); _app.RunEveryMinute(0, () => _app.Entity("binary_sensor.fake_run_every_minute_happened").TurnOn()); _app.RunEveryHour("15:00", () => _app.Entity("binary_sensor.fake_run_every_hour_happened").TurnOn()); _app.RunDaily("23:00:00", () => _app.Entity("binary_sensor.fake_run_daily_happened").TurnOn()); _timer = _app.RunEveryMinute(0, TestTimerDisposal); }