Hi guys,
I’ve got a ZoneMinder camera array and I’ve built a bunch of automations that control the recording state for the cameras (to disable recording when fog, snow or shadows in the wrong spot, etc.) and it works very well but I’ve noticed that when I restart Home Assistant the states can get out of sync. The global and local controls for each camera say that the camera is enabled but the actual ZoneMinder state is disabled.
Because of this I wrote an automation to force an update of ZoneMinder on a restart of Home Assistant to sync everything up. It works, but I’d like to make it more generic (not specifying all the independent cameras within the automation). Right now it looks like this:
- id: cameras_update_states_on_restart
alias: Cameras Update States on Restart
initial_state: true
trigger:
- platform: homeassistant
event: start
action:
- service: switch.turn_{{ 'on' if ( is_state('binary_sensor.cameras_back_yard_global_control', 'on') and is_state('input_boolean.cameras_back_yard_control', 'on') ) else 'off' }}
entity_id: switch.back_yard_state
- service: switch.turn_{{ 'on' if ( is_state('binary_sensor.cameras_west_yard_global_control', 'on') and is_state('input_boolean.cameras_west_yard_control', 'on') ) else 'off' }}
entity_id: switch.west_yard_state
- service: switch.turn_{{ 'on' if ( is_state('binary_sensor.cameras_east_yard_global_control', 'on') and is_state('input_boolean.cameras_east_yard_control', 'on') ) else 'off' }}
entity_id: switch.east_yard_state
- service: switch.turn_{{ 'on' if ( is_state('binary_sensor.cameras_front_door_global_control', 'on') and is_state('input_boolean.cameras_front_door_control', 'on') ) else 'off' }}
entity_id: switch.front_door_state
- service: switch.turn_{{ 'on' if ( is_state('binary_sensor.cameras_garage_global_control', 'on') and is_state('input_boolean.cameras_garage_control', 'on') ) else 'off' }}
entity_id: switch.garage_state
- service: switch.turn_{{ 'on' if ( is_state('binary_sensor.cameras_out_the_back_global_control', 'on') and is_state('input_boolean.cameras_out_the_back_control', 'on') ) else 'off' }}
entity_id: switch.out_the_back_state
- service: switch.turn_{{ 'on' if ( is_state('binary_sensor.cameras_storage_room_global_control', 'on') and is_state('input_boolean.cameras_storage_room_control', 'on') ) else 'off' }}
entity_id: switch.storage_room_state
Lots of copy/pasta and it makes me feel dirty. Also, if I happen to add more cameras I have to keep this automation sync’d up with the camera list which sucks. I’d much rather have a generic automation that applies to all cameras in the group (rolling through each one and setting the state) but I can’t wrap my head around how to do that or if it’s even possible since the entity_id
and service
are two separate things…
Anyone have any ideas?
//edit
added condition
s to each action
so it only triggers the switch update on a mismatch between the HA state and the ZM state.
//edit2
removed condition
s because it doesn’t work how I thought it would.