this post was submitted on 05 Aug 2023
9 points (90.9% liked)

homeassistant

12107 readers
64 users here now

Home Assistant is open source home automation that puts local control and privacy first. Powered by a worldwide community of tinkerers and DIY enthusiasts. Perfect to run on a Raspberry Pi or a local server. Available for free at home-assistant.io

founded 1 year ago
MODERATORS
 

It seems I've been using Home Assistant forever -- at least 5 years, maybe 6. I've gotten pretty good at it and have a fairly complex setup. Between YAML and Node-RED, there's generally no problem I can't solve given enough time and brain sweat.

But this weekend I tackled a problem that stumped me involving my Logitech Harmony remote and buttons in lovelace. I wanted to dynamically change the target device based on the family room harmony hub's current activity. So for instance, the Rewind button behaves differently if I'm watching TV versus watching Roku. The different devices have different remote codes which the Harmony software handles easily. I wanted the same dynamic adaptation using the media card I'd built:

https://i.imgur.com/DlVU3kP.png

Try as I might, I just couldn't figure it out. I understood the logic of what I needed to do, but I was getting nowhere making it work in lovelace. Initially I thought it was just a matter of YAML indentation or maybe some small nuance of templating... turns out the solution was a lot more complex than I would have guessed.

I'd heard that ChatGPT could write YAML, so I decided to give it a try. The conversation transcript is below. I didn't include all of ChatGPT's code here, only what eventually worked.

It's totally amazing that we can have an ongoing conversation with ChatGPT to accomplish this sort of thing. And it's also crazy to me that it was so "human" -- trial-and-error, requiring multiple attempts, almost as if a prof and I were sitting shoulder-to-shoulder working through the problem. It's just jaw-dropping to me that the process actually worked.

I hope you Lemmy folks get a charge out of this. The whole concept is fascinating to me.

= = = = = = = = = = = = = =

Me:


Can you help me write yaml for my home assistant lovelace configuration?

ChatGPT:


Of course! I'd be happy to help you write YAML for your Home Assistant Lovelace configuration. Please provide me with some details about what you want to achieve with your Lovelace setup and any specific elements you'd like to include. For example, do you want to add cards for specific entities, integrate custom components, or create a specific theme?

Feel free to provide as much information as you can about your desired layout, and I'll gladly assist you in creating the YAML configuration.

Me:


Here's the existing, working yaml:

        title: Media
        cards:
          - type: custom:fold-entity-row
            head:
              type: section
              label: Activities
            entities:
              - scene.restart_harmony
              - scene.restart_roku
              - scene.sonos
              - scene.watch_htpc
              - scene.watch_roku
              - scene.watch_tv
          - type: horizontal-stack
            cards:
              - type: entity-button
                entity: remote.family_room_harmony_hub
                name: Vol -
                tap_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    command: VolumeDown
                    device: '36446017'
                  target:
                    entity_id: remote.family_room_harmony_hub
                icon: mdi:volume-minus
                icon_height: 40px
                state_color: false
              - type: entity-button
                entity: remote.family_room_harmony_hub
                name: Mute
                tap_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: Mute
                    device: '36446017'
                icon: mdi:volume-mute
                icon_height: 40px
                state_color: false
              - type: entity-button
                entity: remote.family_room_harmony_hub
                name: Vol +
                tap_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: VolumeUp
                    device: '36446017'
                icon: mdi:volume-plus
                icon_height: 40px
                state_color: false
          - type: horizontal-stack
            cards:
              - type: entity-button
                entity: remote.family_room_harmony_hub
                name: Rew
                tap_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: Rewind
                    device: '78728850'
                icon: mdi:rewind
                icon_height: 40px
                hold_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: Rewind
                    device: '78728850'
                state_color: false
              - type: entity-button
                entity: remote.family_room_harmony_hub
                name: Pause
                tap_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: Pause
                    device: '78728850'
                icon: mdi:pause
                icon_height: 40px
                hold_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: Pause
                    device: '78728850'
                state_color: false
              - type: entity-button
                entity: remote.family_room_harmony_hub
                name: Play
                tap_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: Play
                    device: '78728850'
                icon: mdi:play
                icon_height: 40px
                hold_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: Play
                    device: '78728850'
                state_color: false
              - type: entity-button
                entity: remote.family_room_harmony_hub
                name: FFwd
                tap_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: FastForward
                    device: '78728850'
                icon: mdi:fast-forward
                icon_height: 40px
                hold_action:
                  action: call-service
                  service: remote.send_command
                  service_data:
                    entity_id: remote.family_room_harmony_hub
                    command: FastForward
                    device: '78728850'
                state_color: false

For each entity-button listed here, incorporate the yaml below to dynamically change the target device based on the family room harmony hub's current activity.

name: Rew name: Pause name: Play name: FFwd

{% if state_attr('remote.family_room_harmony_hub', 'current_activity'), "Watch TV" %}
device: '78728850'
{% elif state_attr('remote.family_room_harmony_hub', 'current_activity'), "Watch Roku" %}
device: '79906646'
{% endif %}

ChatGPT:


Got it! It looks like you want to dynamically change the device value in your Home Assistant Lovelace configuration based on the current activity of your Harmony Hub.

To achieve this, you can use Lovelace's template feature to create conditional configurations. Here's how you can modify your existing YAML to incorporate the dynamic device based on the current activity:

[ChatGPT provided code here]

With this configuration, the device value for each entity-button will be dynamically set based on the current activity of your Harmony Hub, just like you specified in the provided YAML. Make sure to replace default_device_id_here with a valid default device ID or leave it empty if you don't want to set a default value.

Me (after incorporating the provided code):


I've tried twice but get this error message: "Failed to call service remote/send _command. required key not provided @ data['command!"

ChatGPT:


I apologize for the oversight. It seems there's a mistake in the YAML. The issue is with the service_data_template section where we are specifying the command but not providing the entity_id correctly.

Let's fix the YAML to provide the correct entity_id for each service_data_template. Here's the updated YAML:

[ChatGPT provided code here]

This should resolve the error, and now the YAML should work as expected, dynamically changing the device based on the current activity of your Harmony Hub for the specified entity-button cards.

Me:


I'm still getting the same error.

ChatGPT:


I apologize for the confusion. I see the issue now. The problem lies in the usage of the service_data_template key inside the tap_action and hold_action sections. In the context of a Lovelace card configuration, you cannot use service_data_template directly. Instead, you need to use the data_template key to specify template-based data for service calls.

Let's fix the YAML to use the correct key data_template instead of service_data_template. Here's the updated YAML:

[ChatGPT provided code here]

Make sure to update all the tap_action and hold_action sections in the YAML with the correct data_template key. This should resolve the error, and your Lovelace card configuration should now work as expected, dynamically changing the device based on the current activity of your Harmony Hub.

Me:


The error is still occurring.

ChatGPT:


Apologies for the confusion. Let's try a different approach to achieve the dynamic device based on the current activity. We'll use the data_template feature provided by Home Assistant to dynamically set the device in the service call.

Please replace the previous YAML with the following updated code:

[ChatGPT provided code here]

With this updated code, the dynamic device based on the current activity of your Harmony Hub should work correctly for the specified entity-button cards.

Me:


Still not working. Same error.

ChatGPT:


I apologize for the continued issue. Let's try an alternative approach to ensure the dynamic device is set correctly. We'll use the service feature provided by Home Assistant to dynamically set the service data including device parameter.

Please replace the previous YAML with the following updated code:

[ChatGPT provided code here]

Please try this updated code. It uses the is_state_attr function to check the current activity and set the device accordingly in the service_data for each entity-button. This should resolve the issue and allow the dynamic device to work correctly.

Me:


There's no error message, but now the button presses don't do anything.

ChatGPT:


I apologize for the inconvenience. It seems I missed the target parameter in the service call for the entity-button cards. Let's add the target parameter to the service call to specify the device for the remote.

Please replace the previous YAML with the following updated code:

[ChatGPT provided code here]

With this updated code, the entity-button cards should now send commands to the correct target device based on the current activity of your Harmony Hub. If the issue persists, please verify that the Harmony Hub and other devices are properly integrated with Home Assistant and the remote entity is functioning as expected in the Home Assistant interface.

Me:


Same as last time: there's no error message, but the button presses don't do anything.

ChatGPT:


I apologize for the continued issues. Let's try one more approach to ensure the dynamic device is set correctly. We'll use a script to handle the service call with the correct device based on the current activity.

Please add the following scripts to your Home Assistant configuration:

script:
  harmony_rew:
    alias: "Harmony Rewind"
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.family_room_harmony_hub
          command: Rewind
          device: >
            {% if is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch TV') %}
              78728850
            {% elif is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch Roku') %}
              79906646
            {% endif %}

  harmony_pause:
    alias: "Harmony Pause"
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.family_room_harmony_hub
          command: Pause
          device: >
            {% if is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch TV') %}
              78728850
            {% elif is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch Roku') %}
              79906646
            {% endif %}

  harmony_play:
    alias: "Harmony Play"
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.family_room_harmony_hub
          command: Play
          device: >
            {% if is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch TV') %}
              78728850
            {% elif is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch Roku') %}
              79906646
            {% endif %}

  harmony_ffwd:
    alias: "Harmony Fast Forward"
    sequence:
      - service: remote.send_command
        data_template:
          entity_id: remote.family_room_harmony_hub
          command: FastForward
          device: >
            {% if is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch TV') %}
              78728850
            {% elif is_state_attr('remote.family_room_harmony_hub', 'current_activity', 'Watch Roku') %}
              79906646
            {% endif %}

Then, update the entity-button cards as follows:

[ChatGPT provided code here]

With this updated code, we are using scripts to handle the service calls for each button press. The scripts include the dynamic device selection based on the current activity of the Harmony Hub. This approach should work correctly to send the commands to the appropriate target device.

Please give it a try, and let me know if it resolves the issue. If you encounter any further problems, we'll continue troubleshooting until we get it working as expected.

Me:


That runs, but a dialog box opens prompting for me to click a Run button for the script. I want the action to run when I click the entity button without further clicks to confirm.

ChatGPT:


I apologize for the confusion. The additional dialog box appears because scripts in Home Assistant are designed to allow users to review the action before execution, especially when triggered from the UI. However, in this case, you want to run the script directly without any confirmation.

To achieve this, we can create template switches for each action and use them to trigger the scripts silently. Here's how you can update your configuration:

  1. Add the following template switches to your Home Assistant configuration:
switch:
  - platform: template
    switches:
      harmony_rew_switch:
        friendly_name: "Harmony Rewind Switch"
        value_template: "{{ false }}"
        turn_on:
          service: script.harmony_rew
        turn_off:
          service: script.dummy_script

      harmony_pause_switch:
        friendly_name: "Harmony Pause Switch"
        value_template: "{{ false }}"
        turn_on:
          service: script.harmony_pause
        turn_off:
          service: script.dummy_script

      harmony_play_switch:
        friendly_name: "Harmony Play Switch"
        value_template: "{{ false }}"
        turn_on:
          service: script.harmony_play
        turn_off:
          service: script.dummy_script

      harmony_ffwd_switch:
        friendly_name: "Harmony Fast Forward Switch"
        value_template: "{{ false }}"
        turn_on:
          service: script.harmony_ffwd
        turn_off:
          service: script.dummy_script
  1. Add a dummy script to your configuration. We'll use it to disable the script entity when the dummy script is executed.
script:
  dummy_script:
    alias: "Dummy Script"
    sequence:
      - service: switch.turn_off
        data:
          entity_id: switch.harmony_rew_switch, switch.harmony_pause_switch, switch.harmony_play_switch, switch.harmony_ffwd_switch
  1. Finally, update your Lovelace configuration to use the new template switches:
title: Media
cards:
  - type: horizontal-stack
    cards:
      - type: entity-button
        entity: switch.harmony_rew_switch
        name: Rew
        icon: mdi:rewind
        icon_height: 40px
        state_color: false

      - type: entity-button
        entity: switch.harmony_pause_switch
        name: Pause
        icon: mdi:pause
        icon_height: 40px
        state_color: false

      - type: entity-button
        entity: switch.harmony_play_switch
        name: Play
        icon: mdi:play
        icon_height: 40px
        state_color: false

      - type: entity-button
        entity: switch.harmony_ffwd_switch
        name: FFwd
        icon: mdi:fast-forward
        icon_height: 40px
        state_color: false

With this updated configuration, clicking on the entity buttons should trigger the respective actions without any additional confirmation dialogs. The template switches will trigger the scripts silently when activated.

I hope this resolves the issue. If you encounter any further problems or have more questions, feel free to ask.

Me:


Holy cow, it finally works.

ChatGPT:


Great to hear that it's finally working! I'm glad we could get it sorted out. If you have any more questions or need further assistance with Home Assistant or anything else, don't hesitate to ask. Happy home automation!

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here