Ok, thanks! Hope this update will clarify such situation with sudden triggering because to me it is happening not first time…
See my post above. The sun elevation attribute does not update very often.
EDIT: actually that’s probably not it. Looking at the PR:
The elevation update should still occur every 1° at the times/values your automations are set to trigger.
Unless you missed the 0 to 6° elevation while restarting. Then the next sun elevation update would be 8 minutes later and trigger both automations at the same time.
Template results are not restored when restarting.
So the first time the elevation updates after a restart both the trigger templates will evaluate to true (-32 is below both -2 and -5), triggering the automations.
“Template result” - do you mean status of automation? is it stored somewhere?
After a restart the value of these trigger templates:
value_template: '{{ state_attr(''sun.sun'', ''elevation'') }}'
Will be ‘unknown’ until the sun.sun integration updates.
As soon as sun.sun updates they will become some value. In this case -32°, triggering the automations.
I think the way around this is to create a template sensor for elevation. Then use the entity id of this sensor in a numeric state trigger (rather than a value template as you have now).
Template sensors are restored on restart (I think… I’m almost certain).
I have such sensors for my sonoff POW R2 values (current, energy etc) . Right after HA restart they all are set to ‘unknown’ . Then first MQTT msg arrives from the device to HA the sensors are updated with real values…
Yep. MQTT is a different kettle of fish. You can set the retain flag on those sensors (on the device that sends them) to fix that.
“Retain” is not a flag for sensor config but it should arrive as part of State msg. no?
Just a related FYI:
Phil, any comment on the likely effectiveness of this?
We got sidetracked from the original problem (nothing to do with mqtt).
First, template sensors are not restored after a restart. They only update when the referenced entity or entities update(s.)
Regarding the template trigger, it would act the same was as a numeric_state
trigger. The first update of the reference entity will cause it to evaluate, If true it will fire. After that it has to become not true, and then back to true to fire again.
So, not sure changing to a template sensor & using that in a trigger would matter. If you really don’t want that first transition to cause the automation to run:
condition:
- condition: template
value_template: "{{ trigger.from_state is not none }}"
Thanks. The only issue with that solution would be if there was a restart just before the trigger is due (less likely than the previous issue of restarting after sunset) which would cause a valid trigger to be missed.
Well. I have changed it to exam a sensor.sun_angle instead… will see.
But what worry me is the fact it happened first time for last 7 months I use these automation. I also was experimenting with home-assistant_v2.db yesterday, purging, deleting due to another issue
Could the changes in the db cause misfiring my shutter automation?
This is exactly what I pointed out to you many posts ago. You then went off on a tangent about mqtt.
Phil pointed out a solution that will work as long as you don’t restart within a few minutes of sunset.
Can not catch the None condition. I created a testing automation to see trigger states. But it is confusing.
- alias: sun angle below30 test
trigger:
platform: numeric_state
entity_id: sensor.solar_angle
below: 1.2
action:
service: persistent_notification.create
data_template:
message: >
Trigger from {{ states('trigger.from_state') }}
to {{ states('trigger.to_state') }}
sensor.solar_angle {{ states('sensor.solar_angle') }}
After a restart and fist change of solar_angle the trigger was fired and returned this msg
Trigger from unknown to unknown sensor.solar_angle 0.49
Why does it return from_state and to_state as unknown?
Don’t put the trigger variables in calls to the states function.
data_template:
message: >
Trigger from {{ trigger.from_state }}
to {{ states('trigger.to_state') }}
sensor.solar_angle_simulator- {{ states('sensor.solar_angle') }}
then it says:
- sun angle below30 test: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: ‘trigger’ is undefined