myhomeiot / DahuaVTO

Control Dahua VTO/VTH devices from Home Assistant
GNU General Public License v3.0
147 stars 17 forks source link

Playing audio on another machine failed after last update #15

Closed Buster14 closed 2 years ago

Buster14 commented 2 years ago
id: '1640312308566'
alias: Dahua VTO
description: ''
trigger:
  - platform: event
    event_type: dahua_vto
condition:
  - condition: template
    value_template: '{{ trigger.event.data.Data.State | int in [1, 2] }}'
action:
  - service: media_player.media_play
    target:
      entity_id: media_player.gmpc
mode: queued
max: 10

So i have GMPC server and GMPC player installed on my viewing station NUC machine, The HA is installed on my main server, not the viewing station. The GMPC server's job is to talk to HA in LAN.

On older HA version the above code would trigger doorbell.mp3 on the GMPC player. But now it wont play at all. I'm getting this error :

Error: In 'template' condition: UndefinedError: 'dict object' has no attribute 'State'

When trying to execute this part :

condition: template value_template: '{{ trigger.event.data.Data.State | int in [1, 2] }}'

Any ideas? Hmm maybe next time i'll think twice before updating HA :(

myhomeiot commented 2 years ago

Hello,

You need to filter dahua_vto events with Code field equals to BackKeyLight (another event's which comes from VTO doesn't have field Data.State):

id: '1640312308566'
alias: Dahua VTO
description: ''
trigger:
  - platform: event
    event_type: dahua_vto
    event_data:
      Code: BackKeyLight
condition:
  - condition: template
    value_template: '{{ trigger.event.data.Data.State | int in [1, 2] }}'
action:
  - service: media_player.media_play
    target:
      entity_id: media_player.gmpc
mode: queued
max: 10
Buster14 commented 2 years ago

That seems to do the trick! Many thanks again @myhomeiot Just wondering, was this change caused by home assistant or the plugin? So next time i will think twice before updating something again.

myhomeiot commented 2 years ago

Just wondering, was this change caused by home assistant or the plugin? So next time i will think twice before updating something again.

This was caused by changes in Home Assistant which was introduced in release 2021.10 but until release 2022.2 it's process templates as usual and log the warning, more info about this change you can read here

Anyway it's good idea to read release notes before update and sometimes check Home Assistant log.

Buster14 commented 2 years ago

Hi, sorry to reopen this again. Since windows based gmpc is rather unstable, i decided to get sonos play 1. But i cant seem to get this working. Sonos is detected and i can play audio through media player in HA. I have tried 2 codes :

id: '1640312308566'
alias: Dahua VTO
mode: queued
max: 10
description: ''
trigger:
  - platform: event
    event_type: dahua_vto
    event_data:
      Code: BackKeyLight
condition:
  - condition: template
    value_template: '{{ trigger.event.data.Data.State | int in [1, 2] }}'
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.family_room
    data:
      media_content_id: media-source://media_source/local/Doorbell.mp3

And the other one :

alias: Dahua VTO
mode: queued
trigger:
- platform: event
  event_type: dahua_vto
  event_data:
    Code: BackKeyLight
action:
- choose:
    - conditions: 
      - condition: template
        value_template: "{{ trigger.event.data.Data.State | int in [1, 2] }}"
      sequence:  
      - service: media_player.play_media
        target:
          entity_id: media_player.family_room
        data:
          media_content_id: media-source://media_source/local/Doorbell.mp3
          media_content_type: audio/mpeg
        metadata:
          title: Doorbell.mp3
          thumbnail: null
          media_class: music
          children_media_class: null
          navigateIds:
            - {}
            - media_content_type: app
              media_content_id: media-source://media_source
  default:
    - service: media_player.play_media
      target:
        entity_id: media_player.family_room
      data:
        media_content_id: media-source://media_source/local/Doorbell.mp3
        media_content_type: audio/mpeg

But neither is working, any thoughts?

myhomeiot commented 2 years ago

Hello, you should run the media service call alone (outside from automation) and check if it's working at all, you can run automation from Home Assistant automation menu. it's will skip the trigger and will run action section:

  - service: media_player.play_media
    target:
      entity_id: media_player.family_room
    data:
      media_content_id: media-source://media_source/local/Doorbell.mp3
Buster14 commented 2 years ago

Thats the problem, with above 2 codes the automation doesn't show up on the GUI (even though it doesnt show up any errors), so i cannot test it directly. I can however, run the media through media > local media > doorbell.mp3 using sonos and it plays just fine. Any idea how to make above automation show up in the automation page?

Buster14 commented 2 years ago

I managed to run it on the GUI, turn out there's another automation that's preventing it from appearing :

id: '1650994964857'
alias: Dahua-VTO
description: ''
trigger:
  - platform: event
    event_type: dahua_vto
    event_data:
      Code: BackKeyLight
condition:
  - condition: template
    value_template: '''{{ trigger.event.data.Data.State | int in [1, 2] }}'''
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.family_room
    data:
      media_content_id: media-source://media_source/local/Doorbell.mp3
      media_content_type: audio/mpeg
    metadata:
      title: Doorbell.mp3
      thumbnail: null
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://media_source
mode: queued

When i run automation with this, it works, but pressing the doorbell directly hasnt worked yet.. Maybe need action>choose?

When i test the condition above i got this error :

Error occurred while testing condition
template value should be a string for dictionary value @ data['value_template']. Got None
myhomeiot commented 2 years ago

Latest versions of Home Assistant has automation debugger, try to use it in order to understand what happened.

You can remove mode: queued or change it to mode: parallel maybe first triggered automation stack at service call and other just waiting in the queue, more about automation modes you can read here. You get error when you test the condition because trigger.event.data defined only when automation triggered by event from Dahua VTO.

Buster14 commented 2 years ago

Sorry Just got back from a trip to test this again. It still doesnt work even with parallel, so i'm not sure which part is causing this,

- id: '1650994964857'
  alias: Dahua-VTO
  description: ''
  trigger:
  - platform: event
    event_type: dahua_vto
    event_data:
      Code: BackKeyLight
  condition:
  - condition: template
    value_template: '{{ trigger.event.data.Data.State | int in [1, 2] }}'
  action:
  - service: media_player.play_media
    target:
      entity_id: media_player.family_room
    data:
      media_content_id: media-source://media_source/local/Doorbell.mp3
      media_content_type: audio/mpeg
    metadata:
      title: Doorbell.mp3
      thumbnail:
      media_class: music
      children_media_class:
      navigateIds:
      - {}
      - media_content_type: app
        media_content_id: media-source://media_source
  mode: parallel

edit : nevermind after tracking down the logs, turns out the doorbell.mp3 somehow missing after reboots, its working now. i will monitor it again after few days hopefully this is the final code. thanks again

Buster14 commented 2 years ago

Hi, this afternoon strangely it fails again, but i answered with my phone almost at the same time, maybe 5 secs difference,

any idea why?

dahuaerror