HADashboard new widget Hagraph. (livegraph from HA data)

I have made 3 new widgets for HAdashboard.

this is the first 1 you can use in HAdashboard. if it is liked enough, maybe that @aimc will take it up in the Dashboard, but till then there is no risk to use it.
You dont need to edit any existing files for this widget.

create a subdir hagraph in your widget dir.
make a file with the next code and save it as hagraph.coffee

class Dashing.Hagraph extends Dashing.Widget

  constructor: ->
    super
    @queryState()

  @accessor 'value',
    get: -> if @_value then (if isNaN(Math.round(@_value)) then @_value else @_value ) else "??"
    set: (key, value) -> 
      if typeof @_value is 'undefined'
        today = new Date
        @_old_x=Math.round(today.getTime()/1000)
        @_my_list = [{x: @_old_x, y: parseFloat(value)}]
        @_value = value
      else
        today = new Date
        @_old_x=Math.round(today.getTime()/1000)
        @_my_list.push ({x: @_old_x, y: parseFloat(value)})
        @_value = value
      if typeof @graph isnt 'undefined'
        @graph.series[0].data = @_my_list
        @graph.render()
      
  queryState: ->
    $.get '/homeassistant/sensor',
      widgetId: @get('id'),
      deviceId: @get('device')
      (data) =>
        json = JSON.parse data
        @set 'value', json.value

  ready: ->
    if @get('bgcolor')
      $(@node).css("background-color", @get('bgcolor'))
    else
      $(@node).css("background-color", "#444")
    container = $(@node).parent()
    # Gross hacks. Let's fix this.
    width = (Dashing.widget_base_dimensions[0] * container.data("sizex")) + Dashing.widget_margins[0] * 2 * (container.data("sizex") - 1)-10
    height = (Dashing.widget_base_dimensions[1] * container.data("sizey"))-55
    @graph = new Rickshaw.Graph(
      element: @node
      width: width
      height: height
      renderer: @get('renderer')
      series: [
        {
        color: "#000",
        data: @_my_list
        }
      ]
    )

    @graph.series[0].data = @get('points') if @get('points')

    x_axis = new Rickshaw.Graph.Axis.Time(graph: @graph)
    y_axis = new Rickshaw.Graph.Axis.Y(graph: @graph, tickFormat: Rickshaw.Fixtures.Number.formatKMBT)
    @graph.render()

  onData: (data) ->

make a file with the next code and save it as hagraph.html

<h1 class="title" data-bind="title"></h1>
<h1 class="title2" data-bind="title2"></h1>

<h2 class="value" data-bind="current | prettyNumber | prepend prefix"></h2>

make a file with the next code and save it as hagraph.scss

// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color:  #dc5945;

$title-color:       rgba(255, 255, 255, 0.7);
$moreinfo-color:    rgba(255, 255, 255, 0.3);
$tick-color:        rgba(0, 0, 0, 0.4);


// ----------------------------------------------------------------------------
// Widget-graph styles
// ----------------------------------------------------------------------------
.widget-hagraph {

  background-color: $background-color;
  position: relative;


  svg {
    position: absolute;
    opacity: 1;
    fill-opacity: 0.4;
    left: 5px;
    top: 50px;
  }

  .title, .value {
    position: relative;
    z-index: 99;
  }

  .title {
    color: #fff;
    margin-bottom: 5px;
    margin-top: 5px;
  }

  .title2 {
    color: #fff;
    margin-bottom: 10px;
    margin-top: 0px;
  }

  .more-info {
    color: $moreinfo-color;
    font-weight: 600;
    font-size: 100%;
    margin-top: 0;
  }

  .x_tick {
    position: absolute;
    bottom: 0;
    .title {
      font-size: 80%;
      color: $tick-color;
      opacity: 1;
      padding-bottom: 3px;
    }
  }

  .y_ticks {
    font-size: 100%;
    fill: $tick-color;
    fill-opacity: 1;
  }

  .domain {
    display: none;
  }

}

now you are ready to put your graph into your dashboard.

in your dashboard you need to use it like this:

    <li data-row="2" data-col="1" data-sizex="2" data-sizey="2">
      <div data-id="your_sensor_id" data-view="Hagraph" data-title="any title text" data-title2="more title text" data-renderer="area" data-bgcolor="#2266CC"></div>
    </li>

you can choose the next options for your renderer (graph type)

  1. area
  2. line
  3. bar
  4. scatterplot

at this moment it isnt time based but inputbased. i am working on making it timebased. i will keep you posted for the neccesary changes.
the graph is not persistent (yet) so it only collects data as long as you have your dashboard opened.
I am thinking about how to put in a persitent option.
but i am not sure how to implement that. would i save just 1 day, 1 week or everything, and then how and where.

below you see an example how it could look:

6 Likes

I edited the code in the first post.

the graph is now time based.
so even when you dont get data it will calculate the graph.

i also took out some obsolete codeparts which caused an error.

Awesome! Is just what I was looking for before I implement HADashboard…

1 Like

Nice nice … but the part how to put this in hadashboard is missing here … can u post it again?

sorry, i dont understand you.

every information is there. which part dont you understand?

I mean this:

… the part how to put the graphs in the config of hadashboard.

sorry missed that :wink:

i edited the post and its there again :wink:

Thanks :slight_smile: thats kind … really nice project dude! do u have some samples screenshots of good dashboards? like : show your dashboard thread?

Do i have to restart the docker each time I add widgets?

the HAdashboard is made by @aimc
there is his topic on the forum with some more screenshot.

about docker i dont know.
i have never used docker.

1 Like

If you are just editing the erb file you can reload the page and new widgets will show up. If adding a new widget you would probably need to restart the docker image to get them to show up.

Bear in mind that if you want to add new widgets to a docker install you will need to expose the widgets part of the file system. If you don’t know how to do that I would suggest the regular install would work better.

unfortunately i dunno … but i have docker … well… then i might not be able to use it… or do u know how to make this?

or just delete the docker version and install the version without docker :wink:

I have one really strange issue… i thought the dashboard is meant to ruun this on a tablet … but i cannot tab on my alarm button here :slight_smile:
It only works with my mouse.

Here the code from this button:

    <li data-row="4" data-col="1" data-sizex="1" data-sizey="1">
	  <div data-id="cpage1" data-view="ChangePage" data-icon="cogs" data-title="Alarm" data-page="2" data-stagger="false" data-fasttransition="true" data-event-click="onClick"></div>
    </li>

what do i have to do here ?

Sorry - I have no idea - that would probably be a browser issue unrelated to my code. Unfortunately I can;t test it because I don;t have a Windows tablet.

Will this work on HADashboard 2?

No it won’t, but I will be adding equivalent functionality to v2.

4 Likes

Hi,

Two weeks ago I started using Home Assistant on a Raspberry PI 3B with a Hass.io install. I have very limited experience with MQTT and ESP8266, but set up an ESP8266 that publishes data to the embedded MQTT broker in HA and display it without any complications on the frontend of HA.
I was really impressed by the HADashboard which I installed with the AppDaemon3 add-on. It works smoothly, but I would like to also display historical sensor data in a graph on HADashboard.

The current topic with postings from ReneTode seemed to answer all my needs, and I tried to implement the code supplied by ReneTode. Unfortunately I get only a blank black screen or an error message when I load the page. Even though I copied the code from the forum post, maybe I don’t even load or construct the widget properly? I think I don’t have enough experience to implement the last bit of code code in the xxxxxxx.dash config file.

I tried to also follow the instructions from http://appdaemon.readthedocs.io/en/stable/WIDGETDEV.html#base-widgets
to generate a new subdirectory for the Base Widget Hagraph: config/appdaemon/widgets/hagraph and put the different config files there. Maybe I am not doing this right?

Would it be possible to have a look at the “.dash” example code that generated this impressive HADashboard by Rene Tode?

this code was for HA dashboard Version 1
appdaemon 3 doesnt have that part (yet)